• Projects
  • R Packages
  • Publications
  • Graduate Students
  • Links
  • Courses
    • Single-case regression analzes
  • Private blog

On this page

  • Example dataset
  • Descriptives
  • Contrasts
  • For those who love Anovas ;-)

Treatment vs. effect contrasts

regression
statistics
contrasts
Author

Jürgen Wilbert

Published

May 17, 2023

Abstract
Here is a simple example to show the differences between treatment and effect contrasts.

Example dataset

Create a random dataset with criteria y, predictors x1, x2 and gender.

  • y and gender are correlated
  • y and x1 are correlated only if gender is 1
  • y and x2 are correlated only if gender is 1
  • x1 and x2 are correlated
  • x1 and x2 have an interaction effect on y only if gender is 1
set.seed(1234)
n <- 2000
gender <- rep(0:1, each = n/2)
y <- sample(0:10, n, replace = TRUE) + gender * sample(0:10, n, replace = TRUE)
x1 <- sample(0:10, n, replace = TRUE) + gender * y
x2 <- x1 + sample(0:10, n, replace = TRUE) + gender * y
y <- y + (x1 > median(x1) & x2 > median(x2) & gender == 1) * sample(0:10, n, replace = TRUE) * 2

dat <- data.frame(y = y, x1 = x1, x2 = x2, gender = gender)

Descriptives

psych::corr.test(dat)
Call:psych::corr.test(x = dat)
Correlation matrix 
          y   x1   x2 gender
y      1.00 0.79 0.82   0.68
x1     0.79 1.00 0.95   0.74
x2     0.82 0.95 1.00   0.79
gender 0.68 0.74 0.79   1.00
Sample Size 
[1] 2000
Probability values (Entries above the diagonal are adjusted for multiple tests.) 
       y x1 x2 gender
y      0  0  0      0
x1     0  0  0      0
x2     0  0  0      0
gender 0  0  0      0

 To see confidence intervals of the correlations, print with the short=FALSE option
psych::describe(dat)
vars n mean sd median trimmed mad min max range skew kurtosis se
y 1 2000 11.6735 9.829965 8.0 10.58375 7.4130 0 40 40 0.8543788 -0.4209326 0.2198047
x1 2 2000 10.0920 6.802549 9.0 9.67625 7.4130 0 30 30 0.5169160 -0.5958746 0.1521096
x2 3 2000 20.2970 12.883508 17.0 19.30562 13.3434 0 58 58 0.6054280 -0.6904413 0.2880840
gender 4 2000 0.5000 0.500125 0.5 0.50000 0.7413 0 1 1 0.0000000 -2.0009998 0.0111831

Contrasts

The left part of the table is with gender as treatment contrast (0 vs. 1) and the right part with gender as effect contrast (-1 vs. 1)

# Gender has values 0 vs. 1 (treatment contrast)
fit1 <- lm(y ~ gender * x1 * x2, data = dat)

# Gender hast -1 vs. 1 (effect contrast)
dat$gender <- car::recode(dat$gender, "0 = -1; 1 = 1")

fit2 <- lm(y ~ gender * x1 * x2, data = dat)

sjPlot::tab_model(fit1, fit2, show.se = TRUE, show.ci = FALSE, col.order = c("est", "se", "std.est", "p"), digits = 4, dv.labels = c("Treatment contrast<br> for gender", "Effect contrast<br> for gender"))
  Treatment contrast
for gender
Effect contrast
for gender
Predictors Estimates std. Error p Estimates std. Error p
(Intercept) 5.2577 0.6189 <0.001 -4.6274 0.6427 <0.001
gender -19.7703 1.2853 <0.001 -9.8852 0.6427 <0.001
x1 -0.0695 0.1358 0.609 0.6640 0.0851 <0.001
x2 -0.0270 0.0795 0.734 0.4363 0.0482 <0.001
gender × x1 1.4671 0.1702 <0.001 0.7335 0.0851 <0.001
gender × x2 0.9267 0.0963 <0.001 0.4633 0.0482 <0.001
x1 × x2 0.0056 0.0116 0.629 -0.0124 0.0059 0.036
(gender × x1) × x2 -0.0360 0.0118 0.002 -0.0180 0.0059 0.002
Observations 2000 2000
R2 / R2 adjusted 0.738 / 0.737 0.738 / 0.737
  • The intercept in model1 (treatment contrast) is the mean of y for gender 0
  • The intercept in model2 (effect contrast) is the mean of y for all data
  • All predictors in model1 without a gender term are effects for gender 0 while the interactions with a gender term are effects for gender 1
  • All predictors in model2 without a gender term are effects across gender while the interactions with a gender term are the effects of gender (subtracted for gender 0 and added for gender 1).
  • gender has a significant effect on y in both models
  • x1, x2 and x1*x2 only have a significant effect on y in model 2
  • All gender interactions are significant in both models where the effect sizes and the standard errors of model2 are half of the corresponding values in model1, so p is identical.

For those who love Anovas ;-)

fit1  |> car::Anova(type = "III") |> 
  knitr::kable(digits = 3)  |>  kableExtra::kable_classic_2()
Sum Sq Df F value Pr(>F)
(Intercept) 1834.702 1 72.167 0.000
gender 6014.889 1 236.592 0.000
x1 6.660 1 0.262 0.609
x2 2.943 1 0.116 0.734
gender:x1 1889.686 1 74.330 0.000
gender:x2 2352.201 1 92.523 0.000
x1:x2 5.937 1 0.234 0.629
gender:x1:x2 235.983 1 9.282 0.002
Residuals 50642.614 1992 NA NA
fit2  |> car::Anova(type = "III") |> 
  knitr::kable(digits = 3)  |>  kableExtra::kable_classic_2()
Sum Sq Df F value Pr(>F)
(Intercept) 1318.072 1 51.846 0.000
gender 6014.889 1 236.592 0.000
x1 1548.577 1 60.912 0.000
x2 2085.657 1 82.038 0.000
gender:x1 1889.686 1 74.330 0.000
gender:x2 2352.201 1 92.523 0.000
x1:x2 112.251 1 4.415 0.036
gender:x1:x2 235.983 1 9.282 0.002
Residuals 50642.614 1992 NA NA
 
Copyright 2023, Jürgen Wilbert